package com.fan.base.util;

import android.annotation.SuppressLint;
import android.widget.Toast;


/**
 * toast封装
 *
 * @author fan
 * @date 2018/3/31
 */

@SuppressLint("ShowToast")
public class ToastUtil {

    private static Toast toast;

    public static void toastResString(int resId) {
        toast(resId, 0);
    }

    public static void toast(int resId, int duration) {
        toast((String) Tools.getContext().getResources().getText(resId), duration);
    }

    public static void toast(String message) {
        toast(message, 0);
    }

    public static void toast(Object message) {
        toast(String.valueOf(message), 0);
    }

    /**
     * 连续点击，不会多次跳出toast，造成用户体验不好
     *
     * @param message
     * @param duration
     */
    public static void toast(String message, int duration) {
        if (toast == null) {
            if (duration == 0) {
                toast = Toast.makeText(Tools.getContext(), message, Toast.LENGTH_SHORT);
            } else {
                toast = Toast.makeText(Tools.getContext(), message, Toast.LENGTH_LONG);
            }
        } else {
            toast.setText(message);
        }
        toast.show();
    }

}
